package com.sfx.shortLink.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sfx.shortLink.dao.entity.ShortLinkDO;
import com.sfx.shortLink.dto.req.ShortLinkPageReqDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface ILinkMapper extends BaseMapper<ShortLinkDO> {

    @Select("SELECT gid as gid, count(*) as shortLinkCount FROM t_link WHERE enable_status = 0 AND del_flag = 0 AND del_time = 0 AND gid IN (#{gids}) GROUP BY gid;")
    List<Map<String, Object>> getGroupShortLinkCount(@Param("gids") List<String> gids);

    /**
     * 短链接记录汇总数据 totalPv , totalUv , totalUip  汇总数据
     */
    @Update("update t_link set total_pv = total_pv + #{totalPv}, total_uv = total_uv + #{totalUv}, " +
            "total_uip = total_uip + #{totalUip} ,click_num = click_num + #{clickNum} where gid = #{gid} and full_short_url = #{fullShortUrl}")
    void incrementStats(
            @Param("gid") String gid,
            @Param("fullShortUrl") String fullShortUrl,
            @Param("totalPv") Integer totalPv,
            @Param("totalUv") Integer totalUv,
            @Param("totalUip") Integer totalUip,
            @Param("clickNum") Integer clickNum);


    IPage<ShortLinkDO> pageLink(ShortLinkPageReqDTO requestParam);
}